<?php


namespace platform\controllers;


use platform\models\User;
use yii\web\NotFoundHttpException;

class UserController extends BaseController
{
    public $modelClass = 'platform\models\User';

    public $createScenario = User::CREATE_SCENARIO;

    public $updateScenario = User::UPDATE_SCENARIO;


    public function actionAssign($id)
    {
        $model = $this->findModel($id);
        $model->assign();
        return;
    }

    public function actionRevoke($id)
    {
        $model = $this->findModel($id);
        $model->revoke();
        return;
    }

    public function actionAssignedRoles($id)
    {
        $model = $this->findModel($id);

        $items = Array_values($model->getAssignedRoles());
        sort($items);
        return ['items' => $items];
    }


    /**
     * @param $id
     * @return User
     * @throws NotFoundHttpException
     */
    public function findModel($id)
    {
        $model = User::findOne($id);

        if (!$model) {
            throw new NotFoundHttpException('用户不存在');
        }

        return $model;
    }
}